﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml;
using PlugNT.Common;
using PlugNT.Custom;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Global
{
    public partial class Pre_Config : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            if (!IsPostBack)
            {
                InitConfigs();
            }
        }




        #region 初始化配置信息
        public void InitConfigs()
        {
            //网站参数设置
            TextBox_web_name.Text = WebConfig.SiteName;
            TextBox_web_url.Text = WebConfig.SiteUrl;
            TextBox_upload_path.Text = WebConfig.UploadPath;
            TextBox_index_path.Text = WebConfig.IndexPath;
            TextBox_water_path.Text = WebConfig.WaterImagePath;
            TextBox_logo_path.Text = WebConfig.LogoImagePath;
            TextBox_admininterfacekey.Text = WebConfig.AdminInterfaceKey;
            chbComment.Checked = WebConfig.CommentLocked;
            TextBox_memorysize.Text = WebConfig.MemorySize.ToString();

            //自动更新
            chbIsUpdate.Checked = WebConfig.IsAutoUpdate;
            litUpdateCount.Text = "已更新了" + PageBuilder.UpdateCount.ToString() + "次";
            string[] times = WebConfig.AutoDateSpan.Split('-');
            string[] str = new string[8] { "每天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天" };
            for (int i = 0; i < 8; i++)
            {
                dllDate.Items.Add(new ListItem(str[i], i.ToString()));
                if (i.ToString() == times[0])
                {
                    dllDate.SelectedIndex = (i > dllDate.Items.Count) ? dllDate.Items.Count : i;
                }
            }
            for (int i = 1; i < 25; i++)
            {
                dllHour.Items.Add(new ListItem(i.ToString() + "时", i.ToString()));
                if (i.ToString() == times[1])
                {
                    dllHour.SelectedIndex = i - 1;
                }
            }
            for (int i = 1; i < 60; i++)
            {
                dllMinute.Items.Add(new ListItem(i.ToString() + "分", i.ToString()));
                if (i.ToString() == times[2])
                {
                    dllMinute.SelectedIndex = i - 1;
                }
            }
            for (int i = 1; i < 60; i++)
            {
                dllSecond.Items.Add(new ListItem(i.ToString() + "秒", i.ToString()));
                if (i.ToString() == times[3])
                {
                    dllSecond.SelectedIndex = i - 1;
                }
            }

        }
        private string GetDataString()
        {
            return dllDate.SelectedValue + "-" + dllHour.SelectedValue + "-" + dllMinute.SelectedValue + "-" + dllSecond.SelectedValue;
        }
        #endregion


        #region 网站参数设置
        protected void siteUpdate_Click(object sender, EventArgs e)
        {
            string error=ModifyConfigs();
            if (error == "")
            {
                Response.Redirect("../Message.aspx?message=配置文件修改成功！&z=yes");
            }
            else
            {
                Response.Redirect("../Message.aspx?message=配置文件修改失败："+error+"&z=no");
            }
        }
        public string ModifyConfigs()
        {

            NameValue sitename = new NameValue("/configs/sitename", TextBox_web_name.Text.Trim());
            NameValue siteurl = new NameValue("/configs/siteurl", TextBox_web_url.Text.Trim());
            NameValue uploadpath = new NameValue("/configs/uploadpath", TextBox_upload_path.Text.Trim());
            NameValue admininterfacekey = new NameValue("/configs/admininterfacekey", TextBox_admininterfacekey.Text.Trim());
            NameValue indexpath = new NameValue("/configs/indexpath", TextBox_index_path.Text.Trim());
            NameValue waterimagepath = new NameValue("/configs/waterimagepath", TextBox_water_path.Text.Trim());
            NameValue logoimagepath = new NameValue("/configs/logoimagepath", TextBox_logo_path.Text.Trim());
            NameValue commentlocked = new NameValue("/configs/commentlocked", chbComment.Checked.ToString());
            NameValue memorysize = null;
            if (PlugNT.Common.Utils.IsNumeric(TextBox_memorysize.Text.Trim()))
            {
                memorysize = new NameValue("/configs/memorysize", TextBox_memorysize.Text.Trim());
            }
            else
            {
                memorysize = new NameValue("/configs/memorysize", WebConfig.MemorySize.ToString());
            }
            string error = WebSeting.Update(sitename, siteurl, uploadpath, admininterfacekey, indexpath, waterimagepath, logoimagepath, commentlocked, memorysize);

            return error;
        }
        #endregion

        #region 自动更新
        protected void autoUpdate_Click(object sender, EventArgs e)
        {
            WebSeting.UpdateAutoUpdate(chbIsUpdate.Checked, GetDataString());
            //自动更新整站
            AutoUpdate.Start();
            Response.Redirect("../Message.aspx?message=配置自动更新修改成功！&z=yes");
        }
        #endregion


    }
}
